﻿using M_Quality.API.Application.Command.QualityPlanCommand;
using M_Quality.Domain;
using M_Quality.Domain.Dto;
using M_Quality.Repository.Interface;
using MediatR;

namespace M_Quality.API.Application.CommandHandler.QualityPlanCommandHandler
{
    /// <summary>
    /// 根据质检组ID显示质检项目
    /// </summary>
    public class QualityprogramShowHandeler : IRequestHandler<QualityprogramShow,List<QuDto>>
    {
        private readonly IQualityprogromRepositry _qualityprogromRepositry;
        private readonly IQualityinspectionRepository _qualityinspectionRepository;
        private readonly IQualitygroupRepository _qualitygroupRepository;
        public QualityprogramShowHandeler(IQualityprogromRepositry qualityprogromRepositry, IQualityinspectionRepository qualityinspectionRepository, IQualitygroupRepository qualitygroupRepository)
        {
            _qualityprogromRepositry = qualityprogromRepositry;
            _qualityinspectionRepository = qualityinspectionRepository;
            _qualitygroupRepository = qualitygroupRepository;
        }

        public async Task<List<QuDto>> Handle(QualityprogramShow request, CancellationToken cancellationToken)
        {
            var GList=(from a in await _qualityprogromRepositry.GetAllAsync()
                      join b in await _qualitygroupRepository.GetAllAsync()
                      on a.QualitygroupId equals b.QualitygroupId
                      join c in await _qualityinspectionRepository.GetAllAsync()
                      on a.QualityinspectionId equals c.QualityinspectionId
                      select new QuDto()
                      {
                          QualityinspectionId=c.QualityinspectionId,
                          QualityinspectionName=c.QualityinspectionName,
                          QualityinspectionBH=c.QualityinspectionBH,
                          Qualityinspectiontool=c.Qualityinspectiontool,
                          Qualityinspectionmethood=c.Qualityinspectionmethood,
                          Qualityinspectionmetype=c.Qualityinspectionmetype,
                          Standardvalue=c.Standardvalue,
                          QualityprogramId=a.QualityprogramId,
                          QualityPlanCode=a.QualityPlanCode,
                          QualitygroupId=b.QualitygroupId,
                      }).ToList();
            if (request.QualitygroupId>0)
            {
                GList= GList.Where(d=>d.QualitygroupId==request.QualitygroupId).ToList();
            }
            return GList.ToList();
        }
    }
}
